package com.lzx.lizhenxing.common.exception;


import com.lzx.lizhenxing.common.vo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(CommonException.class)
    public CommonResult commonExceptionHandler(CommonException e) {
        // 一般异常信息需要存入日志系统

        log.error(e.getMsg());
        log.error("异常信息：{}", e);

        System.out.println(e.getMsg());
        e.printStackTrace();
        if (e.getCode() != 0) {
            return CommonResult.error(e.getCode(), e.getMsg());
        } else {
            return CommonResult.error(e.getMsg());
        }
    }

    @ExceptionHandler(Exception.class)
    public CommonResult exceptionHandler(Exception e) {
        // 一般异常信息需要存入日志系统
        log.error(e.getMessage());
        System.out.println(e.getMessage());
        e.printStackTrace();
        return CommonResult.error("未知异常，请联系客服");
    }
}
